package system;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Rezan Achmad
 */
public class DbConnection {

    private static String url = "jdbc:mysql://localhost:3306/";
    private static String dbName = "tempe";
    private static String driver = "com.mysql.jdbc.Driver";
    private static String userName = "root";
//    private static String password = "rpl123";
    private static String password = "";
    private static Connection connection = null;
    private static Statement statement = null;

    public static Connection getConnection() {
        if (connection == null) {
            try {
                Class.forName(driver).newInstance();
                connection = (Connection) DriverManager.getConnection(url + dbName, userName, password);
            } catch (SQLException ex) {
                Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return connection;
    }

    public static Statement getStatement() {
        if (statement == null) {
            try {
                statement = (Statement) getConnection().createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return statement;
    }
}
